$(function () {
    init_role_add();
});

/**初始化数据**/
function init_role_add(){
    /**表单验证**/
    var $roleAddForm = $("#role-add-form");
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw'></i> ";
    $roleAddForm.validate({
        rules: {
            roleName: {
                required: true,
                minlength: 3,
                maxlength: 10,
                remote: {
                    url: "role/checkRoleName",
                    type: "get",
                    dataType: "json",
                    data: {
                        roleName: function () {
                            return $("#roleName").val().trim();
                        }
                    }
                }
            },
            remark: {
                maxlength: 50
            },
            menuId: {
                required: true
            }
        },
        messages: {
            roleName: {
                required: icon + "请输入角色名称",
                minlength: icon + "角色名称长度3到10个字符",
                remote: icon + "该角色名已经存在"
            },
            remark: icon + "角色描述不能超过50个字符",
            menuId: icon + "请选择相应菜单权限"
        }
    });
    /**菜单权限树状**/
    $.post(ctx + "menu/menuButtonTree", {}, function (r) {
        if (r.code === 0) {
            var data = r.msg;
            $('#menuTree').jstree({
                "core": {
                    'data': data.children
                },
                "state": {
                    "disabled": true
                },
                "checkbox": {
                    "three_state": false
                },
                "plugins": ["wholerow", "checkbox"]
            });
        } else {
            $JuXue.n_danger(r.msg);
        }
    });
}
/**新增角色保存**/
function saveRolesInfo() {
    var $menuTree = $('#menuTree');
    var ref = $menuTree.jstree(true);
    var menuIds = ref.get_checked();
    $menuTree.find(".jstree-undetermined").each(function (i, element) {
        menuIds.push($(element).closest('.jstree-node').attr("id"));
    });
    $("#menuId").val(menuIds);
    var $roleAddForm = $("#role-add-form");
    var validator = $roleAddForm.validate();
    var flag = validator.form();
    if (flag) {
        $.post(ctx + "role/add", $roleAddForm.serialize(), function (r) {
            if (r.code === 0) {
                $JuXue.n_success(r.msg);
                $JuXue.refreshTable("role-data-table");
                $("#Roles_info").modal("hide");
            } else $JuXue.n_danger(r.msg);
        });
    }else{
        $JuXue.n_warning("请填写完整信息");
    }
}
